<template>
	<view class="countdown">
		<!-- 倒计时 -->
		<view class="countdown-content">
			<view class="text" @tap="cashWithdrawal">提现</view>
			<image src="../static/images/wallet.png" style="width: 100%;" mode="widthFix"></image>
			<view class="countdown-progress" v-if="pid">
				<view class="bottom">
					
				</view>
				<view class="top topProgress" id="topProgress">
					
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				isShare: false,
				pid: undefined
			}
		},
		mounted() {
			let openid = uni.getStorageSync(('openid'))
			this.pid = uni.getStorageSync(('tpid'))
			this.pid = this.pid ? Number(this.pid) : 0
			
			if(this.pid){
				this.$nextTick(()=>{
					this.listenProgress(openid)
				})	
			}
		},
		methods: {
			cashWithdrawal(){
				this.$emit("onCash")
			},
			listenProgress(openid){
				const ele = document.getElementById("topProgress")
				console.log('ele', ele);
				let width = 0
				const timer = setInterval(()=> {
					this.getCheckVisit(openid).then(res=> {
						if(res.code === -1){
							ele.style.width = '100%'
							clearInterval(timer)
							return
						}
						width += 100/15
						if(width > 100){
							width =  100
						}
						ele.style.width = width + '%'
					})
				}, 1000)
			},
			getCheckVisit(openid){
				return new Promise((resolve,reject)=>{
					uni.request({
						url:`${this.$comm.servDomain}/api/activelove/check_visit`,
						method: "POST",
						data: { openid },
						success: (res) => {
							resolve(res.data);
						},fail: (err) => {
							uni.showToast({
								icon:"error",
								title:'网络连接失败',
								mask:true,
								duration:1500
							});
							reject(err);
						}
					});
				})
			},
		}
	}
	
</script>

<style lang="scss">
	.countdown{
		position: fixed;
		top: 50%;
		right: 20rpx;
		z-index: 12;
		color: #fff;
		&-content{
			position: relative;
			width: 120rpx;
			height: 120rpx;
			.text{
				position: absolute;
				top: 40rpx;
				left: 30rpx;
				color: #fff;
				font-weight: bold;
				z-index: 11;
			}
		}
		&-progress{
			width: 100%;
			height: 20rpx;
			overflow: hidden;
			// background-color: #ef6223;
			position: relative;
			.bottom{
				width: 100%;
				height: 20rpx;
				background-color: #fff;
				position: absolute;
				top: 0;
				left: 0;
				padding: 0;
				border-radius: 10rpx;
			}
			.top{
				width: 0%;
				height: 20rpx;
				background-color: #ef6223;
				position: absolute;
				top: 0;
				left: 0;
				border-radius: 10rpx;
			}
		}
	}
</style>